package com.tjetc.service.impl;

import com.tjetc.dao.*;
import com.tjetc.dao.impl.*;
import com.tjetc.domain.Bed;
import com.tjetc.domain.Medicine;
import com.tjetc.domain.Notes;
import com.tjetc.domain.User;
import com.tjetc.service.NotesService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class NotesServiceImpl implements NotesService {
    private NotesDao notesDao = new NotesDaoImpl();
    private BedDao bedDao = new BedDaoImpl();
    private FeeDao feeDao = new FeeDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    private MedicineDao medicineDao = new MedicineDaoImpl();

    @Override
    public boolean firstAdd(Notes notes,Bed bed,int fid) {
        try {
            Db.startTransaction();
            // 1.添加到notes表
            notesDao.firstAdd(notes);
            // 2.修改对应床位信息
            bedDao.updateStatus(bed);
            // 3.修改fee表
            feeDao.finishFee(fid);
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
          Db.close();
        }
        return false;
    }

    @Override
    public boolean add(Notes notes){//记录的同时开药，并扣除该用户账户上的account费用
        try {
            Db.startTransaction();
            // 1.记录
            notesDao.add(notes);
            // 2.获取notes中的mid和num 减少药品数量
            double account = 0.0;
            Medicine medicine = medicineDao.getById(notes.getMid());
            account = medicine.getPrice()*notes.getNum();
            medicineDao.cutDown(medicine.getId(),notes.getNum());
            // 3.获取user,修改金额
            User user = userDao.getById(notes.getUid());
            user.setAccount(user.getAccount()-account);
            userDao.update(user);
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }

    @Override
    public int getCount(int nid) {
        try {
            return notesDao.getCount(nid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Notes> list(PageInfo<Notes> pageInfo, int nid){
        try {
            List<Notes> list = notesDao.list(pageInfo, nid);
            for (Notes notes : list) {
                notes.setUser(userDao.getById(notes.getUid()));
                notes.setMedicine(medicineDao.getById(notes.getMid()));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Notes> listMyNotes(int nid) {
        try {
            return notesDao.listMyNotes(nid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Notes findByNid(int id) {
        try {
            return notesDao.findByNid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
